#pragma once

#include <QWidget>
#include <QScrollArea>
#include <QVBoxLayout>
#include "util/serverhelper.h"
#include "util/usermanager.h"
#include "postcardwidget.h"

namespace Ui {
class CommunityPage;
}

namespace WordMemory
{
    class CommunityPage : public QWidget
    {
        Q_OBJECT

    public:
        explicit CommunityPage(QWidget *parent = nullptr);
        ~CommunityPage();
        void refreshPageData();

    signals:
        void viewPostDetails(int postId);

    private slots:
        void onGetCommunityPostsFinished(bool success, const QList<CommunityPost> &posts, const QString &error);
        void onToggleLikePostFinished(bool success, int postId, int newLikesCount, bool isLiked, const QString &error);
        void onRecordSharePostFinished(bool success, int postId, int newSharesCount, const QString &error);

        void handleCardClicked(int postId);
        void handleLikeClickedOnCard(int postId);
        void handleShareClickedOnCard(int postId);

        void on_refreshButton_clicked();

    private:
        void setupUi();
        void setupConnections();
        void loadPosts();
        void clearPostsLayout();

        QMap<int, PostCardWidget*> postCardMap;

        Ui::CommunityPage *ui;
        ServerHelper* serverhelper;

        QVBoxLayout *postsLayout;
        QPushButton *refreshButton;
    };
}
